import type { AxiosInstance, AxiosRequestConfig } from "axios";
import { requestInterceptors, responseInterceptors } from "@/config/interceptorsConfig";
import axios from "axios";
import type { ResultData } from "@/typings";

const axiosConfig = {
  baseURL: import.meta.env.VITE_PROXY as string,
  timeout: 50000
};

class HttpService {
  private service: AxiosInstance;

  public constructor(axiosConfig: AxiosRequestConfig) {
    this.service = axios.create(axiosConfig);

    this.service.interceptors.request.use(requestInterceptors);

    this.service.interceptors.response.use(responseInterceptors);
  }

  get<T>(url: string, config?: AxiosRequestConfig): Promise<ResultData<T>> {
    return this.service.get(url, config);
  }
  post<T>(url: string, data?: object, config?: AxiosRequestConfig): Promise<ResultData<T>> {
    return this.service.post(url, data, config);
  }
  put<T>(url: string, data?: object, config?: AxiosRequestConfig): Promise<ResultData<T>> {
    return this.service.put(url, data, config);
  }
  delete<T>(url: string, config?: AxiosRequestConfig): Promise<ResultData<T>> {
    return this.service.delete(url, config);
  }
}

export default new HttpService(axiosConfig);
